package com.healthcloud.service.impl;

import java.util.ArrayList;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.healthcloud.dao.ExposureHistoryMapper;
import com.healthcloud.entity.ExposureHistory;
import com.healthcloud.service.ExposureHistoryService;

/**
 * Created by XYC on 2015/7/15.
 */
@Service
public class ExposureHistoryServiceImpl implements ExposureHistoryService {

    @Autowired
    private ExposureHistoryMapper exposureHistoryMapper;

    @Override
    public ExposureHistory saveOrUpdata(ExposureHistory exposureHistory) {
        ExposureHistory exposureHistoryTemp = new ExposureHistory();
        exposureHistoryTemp.setIdCard(exposureHistory.getIdCard());
        ArrayList<ExposureHistory> exposureHistories = exposureHistoryMapper.findByEntityWithoutPage(exposureHistoryTemp);
        if (exposureHistories.isEmpty()) {
            exposureHistory.setCreatedTime(new Date());
            exposureHistory.setUpdatedTime(new Date());
            exposureHistoryMapper.save(exposureHistory);
        }else {
            exposureHistory.setUpdatedTime(new Date());
            exposureHistory.setCreatedTime(exposureHistories.get(0).getCreatedTime());
            exposureHistory.setId(exposureHistories.get(0).getId());
            exposureHistoryMapper.update(exposureHistory);
        }
        return exposureHistory;
    }

    @Override
    public ExposureHistory findByEntity(ExposureHistory exposureHistory) {
        ArrayList<ExposureHistory> exposureHistories = exposureHistoryMapper.findByEntityWithoutPage(exposureHistory);
        if (exposureHistories.isEmpty()){
            return null;
        }
        return exposureHistories.get(0);
    }
}
